package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcLongRangeCommentsMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.LongRangeServiceOrderStatusConstant;
import com.csun.cmny.provider.model.constant.ServiceOrderConstant;
import com.csun.cmny.provider.model.domain.OmcComments;
import com.csun.cmny.provider.model.domain.OmcLongRangeComments;
import com.csun.cmny.provider.model.domain.OmcLongRangeServiceOrder;
import com.csun.cmny.provider.model.domain.OmcServiceOrder;
import com.csun.cmny.provider.model.dto.comments.CommentsQueryDto;
import com.csun.cmny.provider.model.dto.comments.CommentsSaveDto;
import com.csun.cmny.provider.model.vo.comments.CommentsVo;
import com.csun.cmny.provider.model.vo.longRangeServiceOrder.LongRangeCommentsVo;
import com.csun.cmny.provider.service.OmcLongRangeCommentsService;
import com.csun.cmny.provider.service.OmcLongRangeServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcLongRangeCommentsServiceImpl extends BaseService<OmcLongRangeComments> implements OmcLongRangeCommentsService {

    @Resource
    private OmcLongRangeServiceOrderService longRangeServiceOrderService;

    @Resource
    private OmcLongRangeCommentsMapper longRangeCommentsMapper;

    @Override
    public int addComments(CommentsSaveDto commentsSaveDto, LoginAuthDto loginAuthDto) {
        /*校验订单是否存在*/
        OmcLongRangeServiceOrder omcServiceOrder = longRangeServiceOrderService.selectByKey(commentsSaveDto.getServiceOrderId());
        if (PublicUtil.isEmpty(omcServiceOrder)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032005);
        }

        //订单评论状态状态修改为已评论
        omcServiceOrder.setIsComments(LongRangeServiceOrderStatusConstant.HAVE_COMMENTS);
        longRangeServiceOrderService.update(omcServiceOrder);


        /*校验是否评论过*/
        OmcLongRangeComments comments = selectByKey(commentsSaveDto.getServiceOrderId());

        if(PublicUtil.isNotEmpty(comments)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032034);
        }

        OmcLongRangeComments omcLongRangeComments = new OmcLongRangeComments();

        BeanUtils.copyProperties(commentsSaveDto,omcLongRangeComments);

        omcLongRangeComments.setGroupId(loginAuthDto.getGroupId());
        omcLongRangeComments.setUserId(loginAuthDto.getUserId());
        omcLongRangeComments.setOperator(loginAuthDto.getUserName());
        return save(omcLongRangeComments);
    }

    @Override
    public List<LongRangeCommentsVo> selectComments(CommentsQueryDto commentsQueryDto) {
        return longRangeCommentsMapper.selectComments(commentsQueryDto);
    }
}
